#include "makeThread.h"

//下载子线程 
void* getsThread(void* arg){
    threadShare_t* pThreadShare = (threadShare_t*)arg;
    //建立tcp连接
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(sockfd,-1,"socket");
    struct sockaddr_in addr;
    bzero(&addr,sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(pThreadShare->ip);
    addr.sin_port = htons(atoi(pThreadShare->port));
    /* printf("ip = %s,port = %s\n",pThreadShare->ip,pThreadShare->port); */
    int ret = connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect");
    //进行token验证,同时发送命令
    int vret = tokenVerify(sockfd,pThreadShare->id,
                           pThreadShare->token,
                           pThreadShare->command);
    if(vret != 0){
        //token验证不通过
        printf("download failure!\n");
        return NULL;
    }
    //由于是新的连接,所以需要发送当前工作目录给服务端
    train_t train;
    memset(&train,0,sizeof(train_t));
    train.length = strlen(pThreadShare->curDirPath)+1;
    send(sockfd,&train.length,sizeof(train.length),0);
    memcpy(train.data,pThreadShare->curDirPath,train.length);
    send(sockfd,&train.data,train.length,0);
    //切分命令和参数
    char tempCommand[4096] = {0};
    memcpy(tempCommand,pThreadShare->command,strlen(pThreadShare->command));
    char* order;
    int arglen;
    arglen = 0;
    char* saveptr;
    char argv[4096] = {0};
    order = strtok_r(tempCommand," ",&saveptr);
    memcpy(argv,saveptr,strlen(saveptr));
    clientExecGets(sockfd,argv);
    free(pThreadShare);
    close(sockfd);
    pthread_exit(NULL);
}    

//上传子线程  
void* putsThread(void* arg){
    threadShare_t* pThreadShare = (threadShare_t*)arg;
    //建立tcp连接
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(sockfd,-1,"socket");
    struct sockaddr_in addr;
    bzero(&addr,sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(pThreadShare->ip);
    addr.sin_port = htons(atoi(pThreadShare->port));
    int ret = connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect");
    //进行token验证,同时发送命令
    int vret = tokenVerify(sockfd,pThreadShare->id,
                           pThreadShare->token,
                           pThreadShare->command);
    /* printf("5 .pThreadShare->pCmdShare->command = %s\n",pThreadShare->command); */
    if(vret != 0){
        //token验证不通过
        printf("download failure!\n");
        return NULL;
    }
    //由于是新的连接,所以需要发送当前工作目录给服务端
    train_t train;
    memset(&train,0,sizeof(train_t));
    train.length = strlen(pThreadShare->curDirPath)+1;
    send(sockfd,&train.length,sizeof(train.length),0);
    memcpy(train.data,pThreadShare->curDirPath,train.length);
    send(sockfd,&train.data,train.length,0);
    //切分命令和参数
    char tempCommand[4096] = {0};
    memcpy(tempCommand,pThreadShare->command,strlen(pThreadShare->command));
    char* order;
    int arglen;
    arglen = 0;
    char* saveptr;
    char argv[4096] = {0};
    order = strtok_r(tempCommand," ",&saveptr);
    memcpy(argv,saveptr,strlen(saveptr));
    clientExecPuts(sockfd,argv);
    free(pThreadShare);
    close(sockfd);
    pthread_exit(NULL);
}   

//创建下载子线程 
int makeGetsThread(threadShare_t* pThreadShare){
    //创建子线程
    pthread_t pid;
    pthread_create(&pid,NULL,getsThread,pThreadShare);
    //将线程与主线程分离,在线程退出时自动释放资源
    pthread_detach(pid);
    return 0;
} 

//创建上传子线程 
int makePutsThread(threadShare_t* pThreadShare){
    //创建子线程
    pthread_t pid;
    pthread_create(&pid,NULL,putsThread,pThreadShare);
    //将线程与主线程分离,在线程退出时自动释放资源
    pthread_detach(pid);
    return 0;
} 
